package org.s3.project.houcontroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.s3.project.common.R;
import org.s3.project.entity.Merchant;
import org.s3.project.houservice.impl.HouMerchantServiceImpl;
import org.s3.project.service.MerchantService;
import org.s3.project.util.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:29
 */
@CrossOrigin(origins = "*")
@RestController
public class HouMerchantController {
    @Autowired
   private AliOSSUtils aliOSSUtils;
    @Autowired
    private HouMerchantServiceImpl houMerchantService;

    /*查看商家分页
   后台管理模糊查询时间查询*/
    @GetMapping("showsb/{page}")
    public R show(@PathVariable("page")int page, Integer pageSize ,
                  @RequestParam(required = false)String name,
                  @RequestParam(required = false) String create_time,
                  @RequestParam(required = false) String update_time) {
        PageHelper.startPage(page,pageSize);
        List<Merchant> list = houMerchantService.selectMerchant(name,create_time,update_time);
        PageInfo<Merchant> pageInfo= new PageInfo< >(list);
        return new R() .add("pageInfo", pageInfo);

    }

    // 删除商家
    @GetMapping("deletemerchant")
    public Boolean deletemerchant( Integer id) {
        return houMerchantService.removeById(id);
    }

    //批量删除商家
    @GetMapping("deleteMerchants")
    public Boolean deleteMerchantS(String ids ) {
        String [] split = ids.split(",");
        return  houMerchantService.deleteMerchants(Arrays.asList(split));

    }
    // 修改商家信息
    @PostMapping("updatemerchant")
    public Boolean updatemerchant(@RequestBody Merchant merchant) {

        //更新数据库修改时间
        houMerchantService.updateTime(merchant.getId());

        return houMerchantService.updateById(merchant);
    }

    // 修改商家封禁状态
    @GetMapping("updateMerchantState")
    public Boolean updateMerchantState(long id , String status) {
        return houMerchantService.updateMerchantState(id,status);
    }

    /*上传商家头像*/
    @PostMapping("addImg")
    public String addBookimg(@RequestParam("file") MultipartFile file) throws IOException {
        return   aliOSSUtils.upload(file);
    }

    // 新增商家
    @PostMapping("addmerchant")
    public Boolean addMerchant(@RequestBody Merchant merchant) {

        return houMerchantService.save(merchant);
    }




}
